package com.sanluan.einvoice;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.sanluan.einvoice.service.Invoice;
import com.sanluan.einvoice.service.InvoiceExtractor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        Scanner input = new java.util.Scanner(System.in);
        System.out.print("请输入您的大名：");
        String name = input.next();


        File directory = new File("");//设定为当前文件夹
        String tempDir = directory.getAbsolutePath();
        File file = new File(tempDir+"\\原始发票");
        if (!file.exists()) {
            System.out.println("原始发票 目录被删除请新建！");
        }

        File[] pdfFiles = file.listFiles();
        if (null == pdfFiles) {
            System.out.println("原始发票 目录下无任何文件！");
        }

        String outDir = tempDir+"\\处理后发票";
        File outFile = new File(outDir);
        if (!outFile.exists()) {
            outFile.mkdir();
        }

        List<Invoice> list = new ArrayList<Invoice>();
        for (int i=0;i<pdfFiles.length;i++) {
            String fileName = pdfFiles[i].getName();
            if(fileName.endsWith(".pdf")) {
                Invoice invoice = null;
                try {
                    invoice = InvoiceExtractor.extract(pdfFiles[i]);
                    invoice.setName(name);
                    invoice.setChecksum(invoice.getChecksum().substring(invoice.getChecksum().length()-6));
                    invoice.setDate(outDateFormat.format(inDateFormat.parse(invoice.getDate())));

                    if(invoice.getSellerName().contains("滴滴")) {
                        invoice.setCatelog("滴滴");
                    }
                    else if(invoice.getSellerName().contains("石油") || invoice.getSellerName().contains("石化")) {
                        invoice.setCatelog("电子油票");
                    }
                    System.out.println("恭喜您原始发票 【"+fileName+"】处理成功.");
                }
                catch (Exception e) {
                    System.out.println("很遗憾原始发票 【"+fileName+"】格式有误！！！！");
                    continue;
                }
                list.add(invoice);
                File newFile = new File(outDir+"\\"+name+"_"+invoice.getTotalAmount()+".pdf");

                int temp = 1;
                while (newFile.exists()) {
                    newFile = new File(outDir+"\\"+name+"_"+invoice.getTotalAmount()+"("+(temp++)+").pdf");
                }
                Files.copy(pdfFiles[i].toPath(),newFile.toPath());
            }
        }
        ExportParams params = new ExportParams();
        Workbook wb = ExcelExportUtil.exportExcel(params,Invoice.class,list);
        wb.write(new FileOutputStream(outDir+"\\excel.xls"));
    }


    static SimpleDateFormat inDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
    static SimpleDateFormat outDateFormat = new SimpleDateFormat("yyyy/MM/dd");
}

